package com.zhkj.crawl.utils;

import com.alibaba.fastjson.JSON;
import com.zhkj.crawl.config.StringPool;
import com.zhkj.crawl.entity.ResultAmz;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class AmzRankStrFinder {
    public static void main(String[] args) {
//        String str = "636,292 in Home & Kitchen (See Top 100 in Home & Kitchen) 329 in Cleaning Gloves 4,093 in Gloves (DIY & Tools) 8,518 in Household Cleaning Tools";
//        ResultAmz result = new ResultAmz();
//        result.setRankStr(str);
//        result.setRatingStr("2.236 Sternebewertungen");
//        result.setStarStr("4,1 sur 5 étoiles");
//
//        findAndFillNum(result);
//        System.out.println(JSON.toJSONString(result));

    }

    private static void fixErr(){
        String str = "452\t#1,829,182 in Clothing, Shoes & Jewelry (See Top 100 in Clothing, Shoes & Jewelry) #7,641 in Girls' Costumes #148,619 in Girls' Fashion #421,896 in Novelty Clothing\n" +
                "453\t#1,817,361 in Clothing, Shoes & Jewelry (See Top 100 in Clothing, Shoes & Jewelry) #7,640 in Girls' Costumes #147,857 in Girls' Fashion #418,433 in Novelty Clothing\n" +
                "546\t#118,944 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #81 in Baby Girls' Boots #4,862 in Boys' Fashion #6,343 in Girls' Fashion\n" +
                "547\t#251,515 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #136 in Baby Girls' Boots #10,301 in Boys' Fashion #12,735 in Girls' Fashion\n" +
                "548\t#151,515 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #94 in Baby Girls' Boots #6,206 in Boys' Fashion #7,930 in Girls' Fashion\n" +
                "583\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "588\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "589\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "590\t#245,239 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #133 in Baby Girls' Boots #10,041 in Boys' Fashion #12,415 in Girls' Fashion\n" +
                "617\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "618\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "627\t#152,989 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #93 in Baby Girls' Boots #6,280 in Boys' Fashion #8,016 in Girls' Fashion\n" +
                "631\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "632\t#251,515 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #136 in Baby Girls' Boots #10,301 in Boys' Fashion #12,735 in Girls' Fashion\n" +
                "637\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "639\t#152,989 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #93 in Baby Girls' Boots #6,280 in Boys' Fashion #8,016 in Girls' Fashion\n" +
                "640\t#152,989 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #93 in Baby Girls' Boots #6,280 in Boys' Fashion #8,016 in Girls' Fashion\n" +
                "652\t#120,673 in Clothing, Shoes & Accessories (See Top 100 in Clothing, Shoes & Accessories) #80 in Baby Girls' Boots #4,934 in Boys' Fashion #6,426 in Girls' Fashion\n" +
                "926\tnº187,741 en Ropa (Ver el Top 100 en Ropa) nº9 en Chalecos para bebés niña nº59 en Chalecos para niña nº162 en Abrigos para niña\n" +
                "1010\tn. 169,223 in Abbigliamento (Visualizza i Top 100 nella categoria Abbigliamento) n. 59 in Maglieria per bambina n. 72 in Cardigan bambine e ragazze n. 95 in Maglioni bambine e ragazze\n" +
                "1011\tn. 169,223 in Abbigliamento (Visualizza i Top 100 nella categoria Abbigliamento) n. 59 in Maglieria per bambina n. 72 in Cardigan bambine e ragazze n. 95 in Maglioni bambine e ragazze\n" +
                "1054\t#23,713 in Clothing & Accessories (See Top 100 in Clothing & Accessories) #29 in Women's Knee-High Socks #2,080 in Girls' Fashion #2,509 in Novelty Clothing\n" +
                "1077\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1078\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1079\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1080\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1081\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1082\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1083\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1084\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1085\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1086\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1087\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1088\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1089\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1090\t#79,067 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #158 in Men's Cycling Gloves #3,129 in Women's Outdoor Recreation Clothing #3,672 in Men's Outdoor Recreation Clothing\n" +
                "1115\t#47,411 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #557 in Women's Outdoor Recreation Hats & Caps #644 in Women's Sports Hats & Caps #886 in Women's Skullies & Beanies\n" +
                "1224\t158,284 in Clothing (See Top 100 in Clothing) 118 in Baby Girls' Coats & Jackets 125 in Girls' Coats 195 in Girls' Jackets\n" +
                "1574\t350,493 in Beauty (See Top 100 in Beauty) 110 in Eyeshadow Bases 4,330 in Eyeshadows 17,695 in Make-up Brushes & Tools\n" +
                "2188\t#43,787 in Clothing & Accessories (See Top 100 in Clothing & Accessories) #64 in Women's Sun Hats #233 in Fishing Accessories #13,521 in Exercise & Fitness Equipment\n" +
                "2483\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2484\t#63,222 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #25 in Women's Gymnastics Leotards #92 in Ballet Equipment #1,936 in Costumes\n" +
                "2485\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2486\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2487\t#63,222 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #25 in Women's Gymnastics Leotards #92 in Ballet Equipment #1,936 in Costumes\n" +
                "2488\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2489\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2490\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2491\t#63,222 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #25 in Women's Gymnastics Leotards #92 in Ballet Equipment #1,936 in Costumes\n" +
                "2492\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2501\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2533\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2540\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2541\t#63,222 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #25 in Women's Gymnastics Leotards #92 in Ballet Equipment #1,936 in Costumes\n" +
                "2542\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2543\t#36,338 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #35 in Ballet Equipment #231 in Women's Sports Shirts #7,903 in Exercise & Fitness Apparel\n" +
                "2915\t#348,742 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #2,899 in Women's Outdoor Recreation Hats & Caps #24,541 in Men's Hats & Caps #40,881 in Sports & Fitness Clothing\n" +
                "2916\t#348,742 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #2,899 in Women's Outdoor Recreation Hats & Caps #24,541 in Men's Hats & Caps #40,881 in Sports & Fitness Clothing\n" +
                "2917\t#348,742 in Sports & Outdoors (See Top 100 in Sports & Outdoors) #2,899 in Women's Outdoor Recreation Hats & Caps #24,541 in Men's Hats & Caps #40,881 in Sports & Fitness Clothing";

        for (String s : str.split("\n")) {
            ResultAmz result = new ResultAmz();
            String[] split = s.split("\t");
            String id = split[0];
            result.setRankStr(split[1]);
            findAndFillNum(result);
            System.out.printf("update result_amz set rank_1=%s, rank_2=%s, rank_3=%s where id=%s;\n",
                    result.getRank1(), result.getRank2(), result.getRank3(), id);

        }
    }

    public static void findAndFillNum(ResultAmz result){
        try {
            findRank(result);
            findRating(result);
            findStar(result);
        }catch (Exception e){
            log.error(String.format("findAndFillNum【%s】【%s】【%s】", result.getRankStr(), result.getStarStr(), result.getRatingStr()), e);
        }
    }

    private static void findStar(ResultAmz result) {
        if(StringUtils.isBlank(result.getStarStr())){
            return;
        }

        String[] split = findAllNumbers(result.getStarStr());
        if(split != null && split.length > 0){
            BigDecimal star = new BigDecimal(split[0]);
            if(star.compareTo(BigDecimal.TEN) >= 0){
                star = star.divide(BigDecimal.TEN);
            }
            result.setStar(star);
        }
    }

    private static void findRating(ResultAmz result) {
        if(StringUtils.isBlank(result.getRatingStr())){
            return;
        }

        String[] split = findAllNumbers(result.getRatingStr());
        if(split != null && split.length > 0){
            String ratingStr = split[0].replace(StringPool.SPACE, StringPool.EMPTY)
                    .replace(".", StringPool.EMPTY);
            result.setRating(Integer.parseInt(ratingStr));
        }
    }


    private static void findRank(ResultAmz result){
        if(StringUtils.isBlank(result.getRankStr())){
            return;
        }

        String[] split = findAllNumbers(result.getRankStr());
        if(split == null){
            return;
        }
        switch (split.length){
            case 1:
                result.setRank1(Integer.parseInt(split[0]));
                break;
            case 2:
                // 中文
                result.setRank1(Integer.parseInt(split[0]));
                result.setRank2(Integer.parseInt(split[1]));
                break;
            case 3:
                if(isContainChinese(result.getRankStr())){
                    result.setRank1(Integer.parseInt(split[0]));
                    result.setRank2(Integer.parseInt(split[1]));
                    result.setRank3(Integer.parseInt(split[2]));
                }else {
                    result.setRank1(Integer.parseInt(split[0]));
                    result.setRank2(Integer.parseInt(split[2]));
                }
                break;
            case 4:
            case 5:
                result.setRank1(Integer.parseInt(split[0]));
                result.setRank2(Integer.parseInt(split[2]));
                result.setRank3(Integer.parseInt(split[3]));
                break;

        }
    }

    private static String[] findAllNumbers(String findStr) {
        try {
            Pattern pattern = Pattern.compile("[^0-9]+[^0-9,]");
            Matcher matcher = pattern.matcher(findStr);
            String all = matcher.replaceAll(StringPool.SEMICOLON)
                    .replace(StringPool.COMMA, StringPool.EMPTY)
                    .replace(StringPool.HASH, StringPool.EMPTY);

            all = all.trim();
            all = all.startsWith(StringPool.SEMICOLON) ? all.substring(1) : all;

            return all.split(StringPool.SEMICOLON);
        }catch (Exception e){
            log.error(String.format("查找数字出错【%s】", findStr), e);
        }
        return null;
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    private static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
